<template>
    <div>
       <Row type="flex" justify="space-between" align="middle">
            <Col span="15" class="title">模型列表</Col>
            <Col span="9" :style="{textAlign: 'right'}">
                <Button icon="md-add" @click="addModel">添加模型</Button>
                <Button type="default" @click="clickOrderBtn">排序</Button>
            </Col>
       </Row>
       <div class="table-box">
           <Table width="100%" :height="height" border :columns="columns" :data="data" :loading="loading"></Table>
       </div>
    </div>
</template>
<script>
    export default {
        data () {
            var height = this.$util.getClientHeight()-64-51-30-58-15;
            var that = this;

            return {
                loading: true,
                height: height,
                indexObj: {},
                columns: [
                    {
                        title: '排序',
                        key: 'listorder',
                        width: 80,
                        render: (h, params) => {
                            var flag = params.row.flag ? 0 : 1;
                            var input = [
                                h('Input', {
                                    props: {
                                        size: 'small',
                                        value: params.row.listorder
                                    },
                                    on: {
                                        input: (val)=>{
                                            var item = {};
                                            item.listorder = parseInt(val);
                                            item.id = params.row.id;
                                            that.indexArrAdd(item);
                                        }
                                    }
                                }, '')];
                            return h('div', input);
                        }
                    },
                    {
                        title: 'ID',
                        key: 'id',
                        width: 70
                    },
                    {
                        title: '名称',
                        key: 'name',
                        width: 120
                    },
                    {
                        title: '表名称',
                        key: 'tableName',
                        width: 100
                    },
                    {
                        title: '描述',
                        key: 'description',
                        width: 200
                    },
                    {
                        title: '创建者',
                        key: 'createName',
                        width: 100
                    },
                    {
                        title: '创建时间',
                        key: 'createTime',
                        width: 170
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 250,
                        render: (h, params) => {
                            var flag = params.row.flag ? 0 : 1;
                            var buttonAction = [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: ()=>{
                                            that.editModel(params);
                                        }
                                    }
                                }, '编辑'),
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: ()=>{
                                            that.delModel(params.row);
                                        }
                                    }
                                }, '删除'),
                                h('Button', {
                                    props: {
                                        type: 'default',
                                        size: 'small'
                                    },
                                    style: {
                                    },
                                    on: {
                                        click: ()=>{
                                            that.fieldManage(params.row);
                                        }
                                    }
                                }, '字段管理')
                                ];

                            return h('div', buttonAction);
                        }
                    }
                ],
                data: []
            }
        },
        created: function () {
            // 获取模型列表信息
            this.getModelList();
        },
        methods: {
            // 获取模型所有信息列表
            getModelList(){
                var that = this;
                that.$util.ajax({
                    that: that,
                    url: '/model/modellist/',
                    success: function(data){
                        that.data = data.data;
                        that.loading = false;
                    },
                    fail: function(msg){
                        that.$Message.info(msg);
                    }
                })
            },
            // 删除模型
            delModel (data){
                var that = this;
                that.$loading.show('更新中');
                that.$util.ajax({
                    that: that,
                    url: '/model/del/',
                    data: {
                        id: data.id
                    },
                    success: function(data){
                        that.$Message.info('删除成功！');
                        that.getModelList();
                    },
                    fail: function(msg){
                        that.$Message.info(msg);
                    },
                    complete: function(){
                        that.$loading.hide();
                    }
                })
            },
            // 编辑模型
            editModel(params){
                this.$router.push({
                                    path: '/admin/editModel',
                                    query: params.row
                                });
            },
            // 添加菜单
            addModel(){
                this.$router.push({
                                    path: '/admin/addModel',
                                    query: {}
                                });
            },
            // 添加修改排序
            indexArrAdd(item){
                var indexObj = this.indexObj
                if(indexObj[item.id]){
                    indexObj[item.id]['listorder'] = isNaN(item.listorder)?0:item.listorder;
                } else {
                    indexObj[item.id] = item;
                }
            },
            // 点击排序按钮
            clickOrderBtn(){
                var indexObj = this.indexObj;
                var indexArr = [];
                var that = this;

                Object.keys(indexObj).forEach(function(key){
                    indexArr.push(indexObj[key]);
                });
                if(indexArr.length === 0){
                    that.$Message.info('请先修改排序，在提交排序！');
                    return;
                }

                that.$loading.show('更新中');
                that.$util.ajax({
                    that: that,
                    url: '/model/order/',
                    data: indexArr,
                    success: function(data){
                        that.$Message.info(data.msg);
                        that.indexObj = {};
                        that.getModelList();
                    },
                    fail: function(msg){
                        that.$Message.info(msg);
                    },
                    complete: function(){
                        that.$loading.hide();
                    }
                })
            },
            fieldManage(data){
                this.$router.push({
                                    path: '/admin/listField',
                                    query: data
                                });
            }
        }
    }
</script>
<style scoped>
</style>